/*
This do-file will run if the working directory is set to
the directory where the replication data is stored.
*/
 
//Installing packages for graphs
net install grc1leg.pkg
ssc install blindschemes
set scheme plottig
 
use "replicationdata.dta", clear 
 
//Main results, displayed in Figure 1
logit select i.ethnic i.gender i.list i.immigPolicy i.equality i.party i.neither
margins, dydx(*) post
coefplot , baselevels ///
    headings(1.ethnic="{bf:Demographics}" 1.list="{bf:Affirmative action}" ///
    1.immigPolicy="{bf:Immigration Policy}" 1.equality="{bf:Policy}" ///
	1.party="{bf:Party}" 0.neither="{bf:Neither option present}" ///
	, labcolor(navy)) ///
	drop(_cons) xline(0) xtitle("Average marginal effects")
graph export "basemodel.pdf", replace

//Conditional ethnic penalties, displayed in Figure 2
logit select i.ethnic##i.gender i.ethnic##i.list i.ethnic##i.immigPolicy i.ethnic##i.equality  i.party  i.neither
margins ethnic, at(immigPolicy=(1(1)3))
marginsplot, title("Immigration policy") ///
    xtitle("") ///
		xlabel(1 "Skilled" 2 "Rufugees" 3 "Limits") ///
		ytitle("Probability of selecting candidate") ///
	saving("immigxethnicity", replace)
margins ethnic, at(list=(1(1)2))
marginsplot, title("Affirmative action") ///
    xtitle("") ///
	xlabel(1 "Getting involved" 2 "List") ///
	ytitle("Probability of selecting candidate") ///
	saving("affirmxethnicity", replace)
margins ethnic, at(equality=(1(1)2))
marginsplot, title("Racial equality laws") ///
    xtitle("") ///
	ytitle("Probability of selecting candidate") ///
	saving("equalityxethnicity", replace)
margins ethnic, at(gender=(1(1)2))
marginsplot, title("Gender") ///
    xtitle("") ///
	ytitle("Probability of selecting candidate") ///
	saving("genderxethnicity", replace)
	
grc1leg "$dir/figures/immigxethnicity" "$dir/figures/affirmxethnicity" "$dir/figures/equalityxethnicity" ///
    "$dir/figures/genderxethnicity", legendfrom("$dir/figures/immigxethnicity") ///
	cols(2) saving("$dir/figures/interactionstemp",replace)
graph describe 	"$dir/figures/interactionstemp"
graph display, ysize(8)
graph export "$dir/figures/interactions_combined_plot.pdf", replace

////////////////
//Extensions////
////////////////

//Interaction between candidate ethnicity and motivation to control prejudice
logit select i.ethnic##c.mcp  i.list i.immigPolicy i.equality i.gender i.party i.neither c.immigrantsWelfareState_m c.immigSelf_m c.immigEcon_m c.immigCultural_m  c.warmBlack c.warmAsian i.ethnicity_small
margins ethnic, at(mcp=(1(1)5))
marginsplot, ///
	ytitle("Probability of selecting candidate") ///
	title("Candidate ethnicity*MCP") 
graph export "$dir/figures/mcpbyethnicity.png", replace

//Effect of candidate ethnicity in the top quartile of MCP
logit select i.ethnic  i.list i.immigPolicy i.equality i.gender i.party i.neither c.immigrantsWelfareState_m c.immigSelf_m c.immigEcon_m c.immigCultural_m  c.warmBlack c.warmAsian i.ethnicity_small if mcp>=4.5 &mcp<.
margins ethnic

//Interaction between candidate ethnicity and respondent's opinion on whether immigration enriches culture or undermines it
logit select i.ethnic##c.immenrichcult i.list i.immigPolicy i.equality i.gender i.party i.neither c.immigrantsWelfareState_m c.immigSelf_m c.immigEcon_m c.immigCultural_m  c.warmBlack c.warmAsian i.ethnicity_small
logit select i.ethnic i.list i.immigPolicy i.equality i.gender i.party i.neither c.immigrantsWelfareState_m c.immigSelf_m c.immigEcon_m c.immigCultural_m  c.warmBlack c.warmAsian i.ethnicity_small if immenrichcult==7 
margins ethnic

//Robustness checks of MCP results using social desirability scale:
//  Interaction between social desirability scale and candidate ethnicity
logit select i.ethnic##c.socialDesScale  i.list i.immigPolicy i.equality i.gender i.party i.neither c.mcp c.immigrantsWelfareState_m c.immigSelf_m c.immigEcon_m c.immigCultural_m  c.warmBlack c.warmAsian i.ethnicity_small c.missingness c.socialDesScale
margins ethnic, at(socialDesScale=(1(1)5))
	
//  Interaction between social desirability scale and candidate ethnicity, and interaction between social desirability scale and MCP
logit select i.ethnic##c.mcp  i.ethnic##c.socialDesScale i.list i.immigPolicy i.equality i.gender i.party i.neither c.immigrantsWelfareState_m c.immigSelf_m c.immigEcon_m c.immigCultural_m  c.warmBlack c.warmAsian i.ethnicity_small c.missingness 
margins ethnic, at(mcp=(1(1)5))
	
//Correlation between MCP and social desirability scale
corr mcp socialDesScale	

//Interactions between candidate ethnicity and (i) candidate's party, and (ii) respondent's vote intention
logit select i.ethnic##i.party i.list i.immigPolicy i.equality i.gender i.neither 
margins ethnic, at(party=(1(1)2)) 
marginsplot, title("Candidate's party") ///
	saving("$dir/figures/candparty", replace)
logit select i.ethnic##i.vi i.party i.list i.immigPolicy i.equality i.gender i.neither if inlist(vi, 1, 2) 
margins ethnic, at(vi=(1(1)2)) 
marginsplot, title("Respondent's party") ///
	saving("$dir/figures/respparty", replace)
grc1leg "$dir/figures/candparty" "$dir/figures/respparty", legendfrom("$dir/figures/respparty")
graph export "$dir/figures/partyinteractions.png", replace
logit select i.ethnic i.party i.list i.immigPolicy i.equality i.gender i.neither if vi==1 //respondent's party (vote intention)
logit select i.ethnic i.party i.list i.immigPolicy i.equality i.gender i.neither if vi==2 //respondent's party (vote intention)

//Do ethnic minority respondents prefer ethnic minority candidates?
//(i) Does any minority candidate get a better result among any minority voters? 
logit select i.emcand i.list i.immigPolicy i.equality i.gender i.party i.neither if inlist(ethnicity_small, 2, 3, 4)
margins, dydx(*)

//(ii) Does candidate ethnicity matter among respondents who are Pakistani, Bangladeshi, and/or Muslim? 
logit select i.ethnic i.list i.immigPolicy i.equality i.gender i.party i.neither if inlist(profile_ethnicity, 8, 9) | inlist(profile_religion_denom, 12) | inlist(profile_religion, 12)
margins, dydx(*)

//(iii) Does candidate ethnicity matter among respondents who are black Caribbean, black African or mixed black ethnicity?
logit select i.ethnic i.list i.immigPolicy i.equality i.gender i.party i.neither if ethnicity_small==3
logit select ib2.ethnic i.list i.immigPolicy i.equality i.gender i.party i.neither if ethnicity_small==3
margins, dydx(*)


//The Neither experiment: who chooses neither?
logit chooseneither i.ethnic i.list i.immigPolicy i.equality i.gender i.party c.missingness c.socialDesScale c.mcp
margins, dydx(*) post
coefplot, baselevels

